import time

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.maximize_window()
kw = driver.find_element(By.ID, "kw")
kw.send_keys("python")
driver.find_element(By.ID, "su").click()

# 第一种写法
# 滚动到页面底部
js1 = "window.scrollTo(0,document.body.scrollHeight)"
driver.execute_script(js1)

# 滚动到页面顶部
time.sleep(2)
js2 = "window.scrollTo(0,0)"
driver.execute_script(js2)

# 第二种写法
# 滚动到页面底部
time.sleep(2)
js3 = "window.scrollTo({left:0, top:document.body.scrollHeight, behavior:'smooth'})"
driver.execute_script(js3)

# 滚动到顶部
time.sleep(2)
driver.execute_script(js2)

# 聚焦到某个元素位置
time.sleep(2)
target = driver.find_element(By.PARTIAL_LINK_TEXT, "菜鸟教程")
driver.execute_script("arguments[0].scrollIntoView();", target)

time.sleep(3)
driver.quit()
